package com.servlet;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class TestMinio {
    public static void main(String[] args) {

        String endpoint="http://192.168.124.101:9000";
        String accessKey="minioadmin";
        String secretKey="minioadmin";
        String buckeName="hello-minio";

        MinioClient minioClient=MinioClient.builder().endpoint(endpoint).credentials(accessKey,secretKey).build();

        try {
            boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(buckeName).build());
            if (!bucketExists){
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(buckeName).build());

                String policy= """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::%s/*"
                          } ],
                          "Version" : "2012-10-17"
                        }
                        """.formatted(buckeName);
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(buckeName).config(policy).build());
            }

            minioClient.uploadObject(UploadObjectArgs.builder().filename("D:\\1摄影\\25.9.14服表部长\\IMG_0228.jpg").bucket(buckeName).object("IMG_0228.jpg").build());
            System.out.println("上传成功");

        } catch (ErrorResponseException e) {
            throw new RuntimeException(e);
        } catch (InsufficientDataException e) {
            throw new RuntimeException(e);
        } catch (InternalException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (InvalidResponseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (ServerException e) {
            throw new RuntimeException(e);
        } catch (XmlParserException e) {
            throw new RuntimeException(e);
        }


    }
}
